﻿using WeDonekRpc.Helper;

namespace Basic.UserFormService.Calculate.Extend
{
    public static class NumberExtend
    {
        public static int Len ( object[] list )
        {
            if ( list == null )
            {
                return 0;
            }
            return list.Length;
        }
        public static string ToString ( decimal num, string format = "F2" )
        {
            return num.ToString(format);
        }
        public static decimal ParseNumber ( string str )
        {
            if ( decimal.TryParse(str, out decimal result) )
            {
                return result;
            }
            return 0;
        }
        public static decimal Sum ( params object[] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            decimal num = decimal.Zero;
            foreach ( object i in list )
            {
                if ( i == null )
                {
                    continue;
                }
                num += Convert.ToDecimal(i);
            }
            return num;
        }
        public static decimal Max ( params object[] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            decimal num = int.MinValue;
            foreach ( object i in list )
            {
                if ( i == null )
                {
                    continue;
                }
                decimal k = Convert.ToDecimal(i);
                if ( k > num )
                {
                    num = k;
                }
            }
            return num;
        }
        public static decimal Min ( params object[] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            decimal num = int.MaxValue;
            foreach ( object i in list )
            {
                if ( i == null )
                {
                    continue;
                }
                decimal k = Convert.ToDecimal(i);
                if ( k < num )
                {
                    num = k;
                }
            }
            return num;
        }
        public static decimal Average ( params object[] list )
        {
            if ( list.IsNull() )
            {
                return 0;
            }
            decimal num = Sum(list);
            return Math.Round(num / list.Length, 2);
        }
    }
}
